package cn.itsource.hrm.hadler;

import cn.itsource.hrm.client.FileClient;
import feign.Response;
import org.apache.commons.io.IOUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author jinhui
 * @version V1.0.0
 * @date 2020/6/5
 */
@Component
public class RecevieHandler {
    public static final String QUEUE_NAME = "page-agent-2";

    @Autowired
    private FileClient fileClient;
    @Value("${page.home.path}")
    private String path;


    //监听队列
    @RabbitListener(queues =QUEUE_NAME)
    public void  queue_name2(String content ){
        //通过fileid将静态页面下载下来

        InputStream inputStream=null;
        OutputStream outputStream=null;
        try {
            Response response = fileClient.download(content);
            inputStream = response.body().asInputStream();
            outputStream = new FileOutputStream(path);
            IOUtils.copy(inputStream,outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
